package Root;

import java.awt.AWTException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.util.Hashtable;
import java.util.LinkedList;

import A002_Obrazek._A_DaneObrazka;

public class _B_SzukaczINT {
	
	private Robot rob;
	
	public _B_SzukaczINT() {
		try {
			rob = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}

	public void FF (BufferedImage _ZrzutZewnetrzny, Hashtable <String, LinkedList<int[][]>> iWieleSzczepow){
		BufferedImage _zrzut;
		
		if (_ZrzutZewnetrzny == null){
			_zrzut = ZrobZrzutAwaryjny(RectangleFulSizeDisplay ());
		} else _zrzut = _ZrzutZewnetrzny;
		
			for (LinkedList<int[][]> Wszep : iWieleSzczepow.values()){
				
				for (int[][] el : Wszep)
				_A_Find(_zrzut, el);

			}
	}
	
	boolean diag = true;

	private void _A_Find( BufferedImage _zrzut, int [][] el){
		
		if (diag){
			System.out.println("element Szerx/Wys-" + el.length +" / " + el[0].length);

		}
		
		for (int yz = 0; yz < _zrzut.getHeight(); yz++){
			for (int xz = 0; xz < _zrzut.getWidth(); xz++){
			
					int X2 = xz + el.length-1;
					int Y2 = yz + el[0].length-1;
					
					if (X2 < _zrzut.getWidth() && Y2 < _zrzut.getHeight()) 
					if (_zrzut.getRGB(xz, yz) == el[0][0] && 
								_zrzut.getRGB(X2, Y2) == el[el.length-1][el[0].length-1]){ // pierwszy i ostatni punkt
						
						if (diag) System.out.println("POZIOM 1");
												
						if (szansa (_zrzut, xz, yz, el)) {
							if(diag){
								rob.mouseMove(xz, yz);
								try {
									Thread.sleep(1000);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
							System.err.print ("Znalazlem" + " "  +  " XY " + xz + ", " + yz + "\n");
						}
					}
				
				
				
			}
		}
		
	}
	
	private boolean szansa (BufferedImage _zrzut, int xz, int yz, int [][] el){
		int test = 0;
		
		for (int yo = 0; yo < el[0].length-1; yo++){
			for (int xo = 0; xo < el.length-1; xo++){
				
				int xtmpz = xz + xo;
				int ytmpz = yz + yo;
				
				if (xtmpz < _zrzut.getWidth() && ytmpz < _zrzut.getHeight()){
						
					if ( el[xo][yo] != -1 ){
						
						if (PW (_zrzut.getRGB(xtmpz, ytmpz), el[xo][yo]) ){
							test ++;
	
						}
						
					} else test ++;
					
				}
				
			}
		}
		
		if (diag) System.out.println ("Poziom 2-" + test);
		if (test == (el.length-1)*(el[0].length-1)) return true;
		else return false;
	}
	
	private boolean PW (int z, int o){
		//if (diag) System.out.println ("Poziom 3-" + z + "_" + o + "_" + (z==o));

		int _tolerKoloru = 0;
		
	 	int red1 = (z >> 16) & 0xff;
	 	int green1 = (z >> 8) & 0xff;
	 	int blue1 = (z) & 0xff;
	 	
    	//int alpha2 = (Z >> 24) & 0xff;
	  	int red2 = (o >> 16) & 0xff;
	 	int green2 = (o >> 8) & 0xff;
	 	int blue2 = (o) & 0xff;
	 	
	 	if (((Math.abs(red1 - red2)) <= _tolerKoloru) && ((Math.abs(green1 - green2)) <= _tolerKoloru) && ((Math.abs(blue1 - blue2)) <= _tolerKoloru)){
	 		return true;
	 	} else return false;
		
		
		
	}
	
	
	
	private Rectangle RectangleFulSizeDisplay (){
		//System.out.println ("Brak wielkosci zrzutu - wykonam pelny ekran");
	    GraphicsEnvironment env = GraphicsEnvironment. getLocalGraphicsEnvironment(); 
	    GraphicsDevice devices = env.getDefaultScreenDevice();
	    int w = devices.getDisplayMode().getWidth();
	    int h = devices.getDisplayMode().getHeight();
        //System.out.println("Width "+devices.getDisplayMode().getWidth());
        //System.out.println("Height "+devices.getDisplayMode().getHeight()); 
	    return new Rectangle(0, 0, w, h);
	    //GraphicsDevice[] devices = env.getScreenDevices();
	}
	
	private BufferedImage ZrobZrzutAwaryjny(Rectangle rec){
		//System.out.println ("Brak obrazu zrzutu, wykonuje wlasny");
		BufferedImage buf = rob.createScreenCapture(rec);
		return buf;
	}
	
	
	
}
